<?php
 /**
 * 学员上课明细验证
 * @date: 2017年8月1日 下午5:24:57
 * @author: hxz
 */
namespace app\admin\validate;
use think\Validate; 
use app\admin\model\Clbum;
use app\admin\model\StudentCourse;
use app\admin\model\StudentClbum;

class StudentAttend extends Validate{
    // 验证规则
    protected $rule = [
        ['student_id', 'require|number', '学员必须'],
        ['clbum_id', 'require|checkClbum|checkAttend', '班级必须|学员不在此班级|学员已上课'],
        ['deduction_hours', 'require|checkHour|gt:0', '课时必须|课时不足,请充值'],
    ];  

    //检查班级和课程对应
    protected function checkClbum($value,$rule,$data)
    {
        $where['student_id'] = $data['student_id'];
        $where['clbum_id'] = $data['clbum_id'];
        $where['course_id'] = $data['course_id'];
        return StudentClbum::get($where) ? true : false;
    }
    //检查是否上课
    protected function checkAttend($value,$rule,$data)
    {
        return checkAttend($data['clbum_id'], $data['student_id']) ? false : true;
    }
    // 检查课时数
    protected function checkHour($value,$rule,$data)
    {
        $where['student_id'] = $data['student_id'];
        $where['course_id'] = $data['course_id'];
        $studentCourse = StudentCourse::get($where);
        return $studentCourse['remain_hours'] < $data['deduction_hours'] ? false : true;
    }
}